#ifndef SOUNDRECORDER_H
#define SOUNDRECORDER_H

#include <QAudioSink>
#include <QAudioSource>
#include <QDir>
#include <QFile>
#include <QMediaDevices>
#include <QObject>
#include <QStandardPaths>

#include "model/data.h"
#include "toast.h"

/// 语音录制和播放类
/// \brief The SoundRecorder class
///
class SoundRecorder : public QObject
{
    Q_OBJECT
public:
    const QString RECORD_PATH = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/sound/tmpRecord.pcm";
    const QString PLAY_PATH = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/sound/tmpPlay.pcm";

public:
    static SoundRecorder* getInstance();

    /////////////////////////////////////////////////
    /// 录制语音
    /////////////////////////////////////////////////
    void startRecord();
    void stopRecord();

    /////////////////////////////////////////////////
    /// 播放语音
    /////////////////////////////////////////////////
    void startPlay(const QByteArray& content);
    void stopPlay();

private:
    explicit SoundRecorder(QObject *parent = nullptr);

// 录制语音需要的变量
private:
    QFile soundFile;
    QAudioSource* audioSource;

// 播放语音需要的变量
private:
    QFile inputFile;
    QAudioSink *audioSink;
    QMediaDevices *outputDevices;
    QAudioDevice outputDevice;

signals:
    // 录制完毕后发送这个信号
    void soundRecordDone(const QString& path);
    // 播放完毕发送这个信号
    void soundPlayDone();
};

#endif // SOUNDRECORDER_H
